/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/
#ifndef CAN_HSCG_H
#define CAN_HSCG_H

#ifdef __cplusplus
extern "C"{
#endif


/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
/**
* @file           Can_HSCG.h
*/
//#include "ComStackTypes.h"
#include "StandardTypes.h"
#include "Can_HSCG_Cfg.h"
#include "Hscg_InterfaceCanTypes.h"
#include "C1200_Flexcan.h"

/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/


/*==================================================================================================
*                                          CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                      DEFINES AND MACROS
==================================================================================================*/

/*==================================================================================================
*                                      DEFINES AND MACROS
==================================================================================================*/

/**
* @brief The memory class AUTOMATIC shall be provided as empty definition, used for the declaration
*        of local pointers.
*/
#define AUTOMATIC

/**
* @brief The memory class TYPEDEF shall be provided as empty definition. This memory class shall be
*        used within type definitions, where no memory qualifier can be specified. This can be
*        necessary for defining pointer types, with e.g. P2VAR, where the macros require two
*        parameters. First parameter can be specified in the type definition (distance to the memory
*        location referenced by the pointer), but the second one (memory allocation of the pointer
*        itself) cannot be defined at this time. Hence memory class TYPEDEF shall be applied.
*/
#define TYPEDEF

/**
* @brief The compiler abstraction shall provide the NULL_PTR define with a void pointer to zero
*        definition.
*/
#ifndef NULL_PTR
#define NULL_PTR ((void *)0)
#endif

/**
* @brief The compiler abstraction shall define the FUNC macro for the declaration and definition of
*        functions, that ensures correct syntax of function declarations as required by a specific
*        compiler.
*/
#define FUNC(rettype, memclass) rettype

/**
* @brief The compiler abstraction shall define the P2VAR macro for the declaration and definition of
*        pointers in RAM, pointing to variables.
*/
#define P2VAR(ptrtype, memclass, ptrclass) ptrtype *

/**
* @brief The compiler abstraction shall define the P2CONST macro for the declaration and definition
*        of pointers in RAM pointing to constants.
*/
#define P2CONST(ptrtype, memclass, ptrclass) const ptrtype *

/**
* @brief The compiler abstraction shall define the CONSTP2VAR macro for the declaration and
*        definition of constant pointers accessing variables.
*/
#define CONSTP2VAR(ptrtype, memclass, ptrclass) ptrtype * const

/**
* @brief The compiler abstraction shall define the CONSTP2CONST macro for the declaration and
*        definition of constant pointers accessing constants.
*/
#define CONSTP2CONST(ptrtype, memclass, ptrclass) const ptrtype * const

/**
* @brief The compiler abstraction shall define the P2FUNC macro for the type definition of pointers
*        to functions.
*/
#define P2FUNC(rettype, ptrclass, fctname) rettype (*fctname)

/**
* @brief The compiler abstraction shall define the CONST macro for the declaration and definition of
*        constants.
*/
#define CONST(consttype, memclass) const consttype

/**
* @brief The compiler abstraction shall define the VAR macro for the declaration and definition of
*        variables.
*/
#define VAR(vartype, memclass) vartype


/**
* @brief The compiler abstraction for const pointer to function.
*/
#define CONSTP2FUNC(rettype, ptrclass, fctname) rettype (* const fctname)


/**
* @brief The compiler abstraction shall define the FUNC_P2CONST macro for the declaration and 
* definition of functions returning a pointer to a constant
*/
#define FUNC_P2CONST(rettype, ptrclass, memclass) const ptrclass rettype * memclass

/**
* @brief The compiler abstraction shall define the FUNC_P2VAR macro for the declaration and 
* definition of functions returning a pointer to a variable.
*/
#define FUNC_P2VAR(rettype, ptrclass, memclass) ptrclass rettype * memclass

/*==================================================================================================
*                                             ENUMS
==================================================================================================*/
/*==================================================================================================
*                                          CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                      DEFINES AND MACROS
==================================================================================================*/
/**
* @brief Transmit request could not be processed because no
*        transmit object was available
* @implements SymbolDefinitions_enum
*/
#define CAN_BUSY    0x02
/*==================================================================================================
*                                             ENUMS
==================================================================================================*/

/**
* @brief          CAN Controller State Modes of operation.
* @details        States that are used by the several ControllerMode functions
* @implements     Can_ControllerStateType_enum
*/
typedef enum
{
    CAN_CS_UNINIT  = 0U,    /**< @brief CAN controller state UNINIT */
    CAN_CS_STARTED,         /**< @brief CAN controller state STARTED */
    CAN_CS_STOPPED,         /**< @brief CAN controller state STOPPED */
    CAN_CS_SLEEP            /**< @brief CAN controller state SLEEP */
} Can_ControllerStateType;

/**
* @brief          CAN Controller State Modes of operation.
* @details        Error states of a CAN controller
* @implements     Can_ErrorStateType_enum
*/
typedef enum
{
    CAN_ERRORSTATE_ACTIVE = 0U,     /**< @brief The CAN controller takes fully part in communication. */
    CAN_ERRORSTATE_PASSIVE,         /**< @brief The CAN controller takes part in communication, but does not send active error frames. */
    CAN_ERRORSTATE_BUSOFF           /**< @brief The CAN controller does not take part in communication. */
} Can_ErrorStateType;

/**
* @brief          CAN Transceiver modes.
* @details        Operating modes of the CAN Transceiver Driver.
*
*/
typedef enum
{
    CANTRCV_TRCVMODE_NORMAL = 0U, /**< @brief Transceiver mode NORMAL */
    CANTRCV_TRCVMODE_STANDBY,     /**< @brief Transceiver mode STANDBY */
    CANTRCV_TRCVMODE_SLEEP        /**< @brief Transceiver mode SLEEP */
} CanTrcv_TrcvModeType;

/**
* @brief          
* @details   This type shall be used to control the CAN transceiver concerning wake up events and wake up notifications.     
*/
typedef enum
{
	CANTRCV_WUMODE_ENABLE = 0x00,   /* The notification for wakeup events is enabled on the addressed transceiver. */
	CANTRCV_WUMODE_DISABLE = 0x01,  /* The notification for wakeup events is disabled on the addressed transceiver. */
	CANTRCV_WUMODE_CLEAR = 0x02	 	/* A stored wakeup event is cleared on the addressed transceiver. */
} CanTrcv_TrcvWakeupModeType;


/**
* @brief          
* @details   This type denotes the wake up reason detected by the CAN transceiver in detail.
*/
typedef enum
{
	CANTRCV_WU_ERROR	 		= 0, /* Due to an error wake up reason was not detected. This value may only be reported when error was reported to DEM before. */
	CANTRCV_WU_NOT_SUPPORTED 	= 1, /* The transceiver does not support any information for the wake up reason.  */
	CANTRCV_WU_BY_BUS			= 2, /* The transceiver has detected, that the network has caused the wake up of the ECU. */
	CANTRCV_WU_INTERNALLY		= 3, /* The transceiver has detected, that the network has woken up by the ECU via a request to NORMAL mode. */
	CANTRCV_WU_RESET			= 4, /* The transceiver has detected, that the "wake up" is due to an ECU reset. */
	CANTRCV_WU_POWER_ON			= 5, /* The transceiver has detected, that the "wake up" is due to an ECU reset after power on. */
	CANTRCV_WU_BY_PIN			= 6, /* The transceiver has detected a wake-up event at one of the transceiver's pins (not at the CAN bus). */
	CANTRCV_WU_BY_SYSERR		= 7  /* The transceiver has detected, that the wake up of the ECU was caused by a HW related device failure. */
} CanTrcv_TrcvWakeupReasonType;
/*==================================================================================================
*                                STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/
/**
* @{
* @brief          Can_IdType
* @details        Represents the Identifier of an L-PDU. The two most significant bits specify theframe type:
* -00 CAN message with Standard CAN ID
* -01 CAN FD frame with Standard CAN ID
* -10 CAN message with Extended CAN ID
* -11 CAN FD frame with Extended CAN ID
* @implements     Can_IdType_type
*/
typedef  uint16         PduIdType;
typedef  uint32         Can_IdType;
typedef  uint32         PduLengthType; 


/** 
* @brief Variables of this type are used to store the basic information about a PDU of any type,
*        namely a pointer variable pointing to it's SDU (payload), and the corresponding length 
*        of the SDU in bytes.
* @implements PduInfoType_structure
*/
typedef struct
{
    uint8 * SduDataPtr;
    PduLengthType  SduLength;
} PduInfoType;


/**
* @brief          Can_PduType
* @details        Type used to provide ID, DLC, SDU from CAN interface to CAN driver.
*                 HTH or HRH = ID+DLC+SDU.
* @implements     Can_PduType_structure
*/
typedef struct
{
    Can_IdType id; /**< @brief CAN L-PDU = Data Link Layer Protocol Data Unit.
                                             Consists of Identifier, DLC and Data(SDU)  It is
                                             uint32 for CAN_EXTENDEDID=STD_ON, else is uint16.
                                             */
    PduIdType swPduHandle; /**< @brief The L-PDU Handle = defined and placed
                                                     inside the CanIf module layer. Each handle
                                                     represents an L-PDU, which is a constant
                                                     structure with information for Tx/Rx
                                                     processing. */
    uint8 length; /**< @brief DLC = Data Length Code (part of L-PDU that describes
                                            the SDU length). */
    uint8 * sdu; /**< @brief CAN L-SDU = Link Layer Service Data
                                                          Unit. Data that is transported inside
                                                          the L-PDU. */
} Can_PduType;


/**
* @{
* @brief          Can_HwHandleType
* @details        Represents the hardware object handles of a CAN hardware unit.
                  For CAN hardware units with more than 255 HW objects use extended range.
*                    used by "Can_Write" function.
*                 The driver does not distinguish between Extended and Mixed transmission modes.
*                 Extended transmission mode of operation behaves the same as Mixed mode.
* @implements     Can_HwHandleType_type
*/
typedef uint16 Can_HwHandleType;

/**
* @brief          Can_HwType
* @details        This type defines a data structure which clearly provides an Hardware Object 
                    Handle including its corresponding CAN Controller and therefore CanDrv as well 
                    as the specific CanId.
* @implements     Can_HwType_structure
*/

typedef struct
{
    Can_IdType CanId; /**< @brief Standard/Extended CAN ID of CAN L-PDU. */
                                             
    Can_HwHandleType  Hoh; /**< @brief ID of the corresponding Hardware Object Range */
    
    uint8 ControllerId; /**< @brief ControllerId provided by CanIf clearly 
                                            identify the corresponding controller */
                                            
} Can_HwType;




/**
* @brief          Can_HSCG_ObjType
* @details        Used for value received by Tressos interface configuration.
*                 Describes the HOH configuration.
*
*/
typedef enum
    {
        CAN_HSCG_RECEIVE = 0U,    /**< @brief Regular Receive */
        CAN_HSCG_RECEIVE_AF,      /**< @brief Receive with Advanced Features*/
        CAN_HSCG_TRANSMIT    /**< @brief Transmit MB */
    } Can_HSCG_ObjType;


    
/**
* @brief          Can_HSCG_StatusType
* @details        CAN Driver status used for checking and preventing double driver intialization.
*                 CAN_HSCG_UNINIT = The CAN controller is not initialized. The CAN Controller is not participating on the CAN bus.
*                              All registers belonging to the CAN module are in reset state, CAN interrupts are disabled.
*                 CAN_HSCG_READY = Controller has initialized: static variables, including flags;
*                             Common setting for the complete CAN HW unit; CAN controller specific settings for each CAN controller.
*
*/
typedef enum
    {
        CAN_HSCG_UNINIT = 0U, /**< @brief Driver not initialized */
        CAN_HSCG_READY       /**< @brief Driver ready */
    } Can_HSCG_StatusType;

/*==================================================================================================
*                                STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/

/*! @brief FlexCAN bitrate related structures
 */
/* implements  Flexcan_Ip_TimeSegmentType_structure */
typedef struct
{
    uint32 preDivider;      /**< Clock prescaler division factor*/
    uint32 propSeg;         /**< Propagation segment*/
    uint32 phaseSeg1;       /**< Phase segment 1*/
    uint32 phaseSeg2;       /**< Phase segment 2*/
    uint32 rJumpwidth;      /**< Resync jump width*/
} Flexcan_Ip_TimeSegmentType;

/**
* @brief          Configuration of BCAN controller.
* @details        This structure is initialized by Tresos considering user settings.
*                 Used by Can_HSCG_SetBaudrate() API to set the default baudrate after initialization.
*                 and to change the baudrate when the BCAN is in stopped mode.
*
*/



#if 0
    /**
     * @brief INPUT: Configuration of data phase baud rate:
     * - Prescaler divisor: (bit 23-27)
     * - Resynchronization Jump Width: (bit 16-19)
     * - Time Segment 2: (bit 9-12)
     * - Time Segment 1: (bit 0-4)
     *
     * Each parameter value shall be decreased by 1 when it is written into
     * this data structure field.
     */
    uint32 u32DataBaudrateConfig;
    /**
     * @brief INPUT: Enable or disable FD related features of the CAN
     * controller.
     */
    uint8 u8FdEnable;
    /**
     * @brief INPUT: Enable or disable baud rate switch (BRS) at the level
     * of CAN controller.
     */
    uint8 u8ControllerTxBitRateSwitch;
    /**
     * @brief INPUT: Enable or disable Transceiver Delay Compensation:
     * - TRUE: enabled
     * - FALSE: disabled.
     */
    uint8 u8TrcvDelayCompEnable;
    /**
     * @brief INPUT: Enable or disable Transceiver Delay Measurement:
     * - TRUE: enabled
     * - FALSE: disabled.
     *
     * When it is enabled, the secondary sample point is determined by the
     * sum of the transceiver delay measurement plus transceiver delay
     * compensation offset. When it is disabled, the secondary sample point
     * is determined only by the transceiver delay compensation offset.
     */
    uint8 u8TrcvDelayMeasEnable;
    /** @brief INPUT: Value of Transceiver Delay Compensation Offset*/
    uint8 u8TrcvDelayCompOffset;
#endif

typedef struct{
    uint8 fd_enable;
    uint8 u8ControllerTxBitRateSwitch;
    uint8 u8TrcvDelayMeasEnable;
    uint8 u8TrcvDelayCompOffset;
} Can_HSCG_FdConfigType;


typedef struct
{
    Can_HSCG_FdConfigType fd_config;
    Flexcan_Ip_TimeSegmentType bitrate_normal;              /**< The bitrate used for standard frames or for the arbitration phase of FD frames. */
    Flexcan_Ip_TimeSegmentType bitrate_data;                /**< The bitrate used for the data phase of FD frames. */
} Can_HSCG_ControllerBaudrateConfigType;


/**
* @brief          Can_HSCG_HOHType
* @details        Type for storing information about hardware objects.
*                 Used by Can_HSCG_MBConfigContainerType.
*
*/
typedef struct
{
    CONST(uint16, CAN_HSCG_CONST) u16MbCount; /**< @brief Maximum number of message buffers for this HOH */

    CONST(uint8, CAN_HSCG_CONST) u8ControllerId; /**< @brief Controller ID (index into controller address array containing Can_ControllerPtrType). */

    VAR(Can_HSCG_ObjType, CAN_HSCG_VAR) eMBType; /**< @brief Receive/ Receive AF / Transmit */

    VAR(uint8, CAN_HSCG_VAR) u8FdPaddingValue; /**< @brief Padding value for MB > 8 bytes */

    CONST(uint8, CAN_HSCG_CONST) u8CanMainFuncRWPeriodRef; /**< @brief read write period reference */

    CONST(uint8, CAN_HSCG_CONST) u8RWInterface; /**< @brief Rx/Ack interface. Specifies which interface to be used for current HOH. */

    CONST(uint8, CAN_HSCG_CONST) u8EnableTxFrameMac; /**< @brief The parameter is used to enable adding of MAC code to transmitted frames.*/
} Can_HSCG_HOHType;


/**
* @brief          Structure for describing individual BCAN controllers on the chip.
* @details        HRH = Hardware Receive Handle (HRH) is defined and provided by the CAN driver.
*                       Each HRH represents exactly one hardware object.
*                       The HRH can be used to optimize software filtering.
*                 HTH = The Hardware Transmit Handle (HTH) is defined and provided by the CAN driver.
*                       Each HTH represents one or several hardware objects, that are
*                       configured as hardware transmit pool.
*
*/
typedef struct
{
    const uint8 u8MaxBaudRateCount;         /**< @brief Max BaudRate number. */
    const uint8 u8DefaultBaudRateIndex;     /**< @brief Default baudrate index. */
    const Can_HSCG_ControllerBaudrateConfigType * const pControllerBaudrateConfigsPtr;   /**< @brief Pointer to baudrate controller */
    const uint32 u32DrvControllerOptions;   /*< @brief Variable used only by driver. Event Trigger Mode TxProcessing/RxProcessing/BusoffProcessing. */
    const uint8 u8HwCtrl;                   /**< @brief Hardware ID for BCAN controller */
    const uint8 u8CanIfCtrlId;              /**< @brief Controller ID defined by CanIf */
} Can_HSCG_ControllerDescriptorType;

/**
* @brief          Top Level structure containing all Driver configuration.
* @details        A pointer to this structure is transmitted to Can_HSCG_Init() to initalize the driver at startup.
*                 The application selects one of the configurations by using a pointer to one of the elements
*                 of this array as a parameter of the Can_HSCG_Init function.
*
*/
typedef struct
{
    VAR(uint8, CAN_HSCG_VAR) u8ControllersConfigured; /**< @brief Number of Can Controllers configured in Tresos plugin. */

    /*Maximum Object IDs configured */
    VAR(uint32, CAN_HSCG_VAR) u32CanMaxObjectId;

} Can_HSCG_Static_ConfigType;


/**
* @brief          Top Level structure containing all Driver configuration.
* @details        A pointer to this structure is transmitted to Can_HSCG_Init() to initalize the driver at startup.
*                 The application selects one of the configurations by using a pointer to one of the elements
*                 of this array as a parameter of the Can_HSCG_Init function.
*
*/
typedef struct
{
    const Hscg_Can_InitPlatformCmdType * const pPlatfInitConfig;    // Configuration used by platform init command
    const Hscg_Can_InitCmdType * const pChannelInitConfig;          // Configuration used by channel init command
    Hscg_Can_EnhancedRxFifoFilterType** paEnhancedRxFifoFilters;    // Enhanced FIFO Filters
    const Can_HSCG_HOHType * const aHohList;                     // Pointer to the list of hardware objects
    const Can_HSCG_ControllerDescriptorType * const aControllerDescriptors;
    const Can_HwHandleType u8ControllersConfigured;
} Can_HSCG_ConfigType;


/**
* @brief          CanErrorNotification callback input type.
* @details        
*
*/
typedef struct
    {
        volatile Hscg_Can_ErrorNotifType* ErrorInfo; /**< @brief Error description */

        uint8 u8CanIfCtrlId; /**< @brief Controller Id */

        Hscg_Can_NotificationIdType eNotifId; /**< @brief Error type */
    /* @violates @ref Can_h_REF_5 Violates MISRA 2012 Advisory Rule 2.3, False positive */
    } CanErrorNotificationType;


/*==================================================================================================
*                                GLOBAL VARIABLE DECLARATIONS
==================================================================================================*/


/*
    Holds current status/run-time of CAN driver: CAN_HSCG_UNINIT or CAN_HSCG_READY (declared as global variable in "Can_HSCG.c" file).
    (CAN103) After power-up/reset, the Can module shall be in the state CAN_HSCG_UNINIT.
    Covers CAN103
*/
extern VAR(Can_HSCG_StatusType, CAN_HSCG_VAR) Can_HSCG_eDriverStatus;

/* @violates @ref Can_h_REF_4 Configuration data generated externally */
extern CONST(Can_HSCG_Static_ConfigType, CAN_HSCG_CONST) CanStatic_ConfigSet;



/*==================================================================================================
*                                    FUNCTION PROTOTYPES
==================================================================================================*/



FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_GetControllerErrorState( VAR(uint8, AUTOMATIC) Controller,
                                                P2VAR(Can_ErrorStateType, AUTOMATIC, CAN_HSCG_APPL_DATA) ErrorStatePtr
                                              );

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_GetControllerRxErrorCounter ( VAR(uint8, AUTOMATIC) ControllerId,
                                                                                                 P2VAR(uint8, AUTOMATIC, CAN_HSCG_APPL_DATA) RxErrorCounterPtr
                                                                                                );

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_GetControllerTxErrorCounter ( VAR(uint8, AUTOMATIC) ControllerId,
                                                                                                 P2VAR(uint8, AUTOMATIC, CAN_HSCG_APPL_DATA) TxErrorCounterPtr
                                                                                                );

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_GetControllerStatus( VAR(uint8, AUTOMATIC) Controller, P2VAR(Hscg_Can_GetStatusCmdType, AUTOMATIC, CAN_HSCG_APPL_DATA) StatusPtr);

FUNC(void, CAN_HSCG_CODE) Can_HSCG_Init( P2CONST(Can_HSCG_ConfigType, AUTOMATIC, CAN_HSCG_APPL_CONST) Config);

FUNC(void, CAN_HSCG_CODE) Can_HSCG_DeInit(void);

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_GetControllerMode( VAR(uint8, AUTOMATIC) Controller, P2VAR(Can_ControllerStateType, AUTOMATIC, CAN_APPL_DATA) ControllerModePtr);

#if (CAN_HSCG_SET_BAUDRATE_API == STD_ON)
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_SetBaudrate( VAR(uint8, AUTOMATIC) Controller,
                                                       CONST(uint16, AUTOMATIC) BaudRateConfigID
                                                     );
#endif /* (CAN_HSCG_SET_BAUDRATE_API == STD_ON) */


FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_SetControllerMode( VAR(uint8, AUTOMATIC) Controller,
                                                             VAR(Can_ControllerStateType, AUTOMATIC) Transition
                                                           );

FUNC(void, CAN_HSCG_CODE) Can_HSCG_DisableControllerInterrupts( VAR(uint8, AUTOMATIC) Controller);

FUNC(void, CAN_HSCG_CODE) Can_HSCG_EnableControllerInterrupts( VAR(uint8, AUTOMATIC) Controller);

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_Write( VAR(Can_HwHandleType, AUTOMATIC) Hth,
                                                 P2CONST(Can_PduType, AUTOMATIC, CAN_HSCG_APPL_CONST) PduInfo
                                               );

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_CheckWakeup( VAR(uint8, AUTOMATIC) controller);

FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_HSCG_GetFwVersion( const uint8 ** pVersionString, \
                                                                                  P2VAR(uint8, AUTOMATIC, CAN_HSCG_APPL_DATA) pStringLength);

#if (CAN_HSCG_TXPOLL_SUPPORTED == STD_ON)
FUNC(void, CAN_HSCG_CODE) Can_HSCG_MainFunction_Multiple_Write( VAR(uint8, AUTOMATIC) writepoll);
#endif /* (CAN_HSCG_TXPOLL_SUPPORTED == STD_ON) */

#if (CAN_HSCG_RXPOLL_SUPPORTED == STD_ON)
FUNC(void, CAN_HSCG_CODE) Can_HSCG_MainFunction_Multiple_Read( VAR(uint8, AUTOMATIC) readpoll);
#endif /* (CAN_HSCG_RXPOLL_SUPPORTED == STD_ON) */

#if (CAN_HSCG_ERRORNOTIFPOLL_SUPPORTED == STD_ON)
FUNC(void, CAN_HSCG_CODE) Can_HSCG_MainFunction_ErrorNotification(void);
#endif /* (CAN_HSCG_ERRORNOTIFPOLL_SUPPORTED == STD_ON) */



void Can_Hscg_Enhanced_FIFO_Handle(void);







/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/

/*==================================================================================================
*                                          CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                      DEFINES AND MACROS
==================================================================================================*/

/** @brief   STM timer used for performance measurements. */
#ifdef CAN_HSCG_STM_MEASUREMENT_ENABLE
    #define HSCG_STM_BASEADDR          ((uint32)0x43C1A000UL)
    #define HSCG_STM_CR                 (HSCG_STM_BASEADDR)
    #define HSCG_STM_CNT                (HSCG_STM_BASEADDR + 4 )
#endif

#define CAN_HSCG_FILTER_DEFAULT_EMPTY_ADDRESS_U16   ((uint16)0xFFFF)

#define HSCG_CAN_CONTROLLERCONFIG_RXINT_EN_U32   ((uint32)0x00000200U) /**< @brief   Enable interrupt on receive.*/
#define HSCG_CAN_CONTROLLERCONFIG_TXINT_EN_U32   ((uint32)0x00000100U) /**< @brief   Enable interrupt on transmit.*/
#define HSCG_CAN_CONTROLLERCONFIG_BOINT_EN_U32   ((uint32)0x00001000U) /**< @brief   Enable interrupt mode for Bus off.*/
#define HSCG_CAN_REFERENCE_NOT_USED              ((uint8)0xFFU)  /**< @brief   R/W period reference not used */

/*==================================================================================================
*                                             ENUMS
==================================================================================================*/

/**
* @brief          CAN Interrupts state.
* @details        CAN Interrupts state.
*
*/
typedef enum
    {
        CAN_INTERRUPT_DISABLED = 0U,  /*< @brief Interrupts disabled */
        CAN_INTERRUPT_ENABLED         /*< @brief Interrupts enabled */
    } Can_InterruptStateType;

/*==================================================================================================
*                                STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/
    
/**
* @brief          Records the status of a CAN Controller during runtime.
* @details        Records the status of a CAN Controller during runtime.
*
* @note           This structure is not configured by Tresos.
*/
typedef struct
{
    volatile VAR(sint8, CAN_HSCG_VAR) s8IntDisableLevel;                 /*< @brief Storage space for Can_HSCG_DisableControllerInterrupts nesting level */
    volatile VAR(uint8, CAN_HSCG_VAR) u8FirstTxMBIndex;                  /*< @brief Index of the first MB used for Tx for a specific controller. This value is relative to 0 (which is first MB). */
    VAR(Can_InterruptStateType, CAN_HSCG_VAR) eInterruptMode;            /*< @brief Global interrupt authorization state */
    VAR(uint8, CAN_HSCG_VAR) u8CurrentBaudRateIndex;                     /*< @brief Current controller baud rate */
    VAR(Can_ControllerStateType, CAN_HSCG_VAR) eCachedControllerState;   /*< @brief Local copy of controller state */
} Can_HSCG_ControllerStatusType;



typedef struct{
    uint32 Req_Tx;
    uint32 Req_Rx;
    uint32 Resp_Tx;
    uint32 Resp_Rx;
    uint32 u32Return;
    uint32 Cmd;
    uint32 Len;
    uint32 aParam;
}Hscg_CmdBuf_InfoType;


/*==================================================================================================
*                                GLOBAL VARIABLE DECLARATIONS
==================================================================================================*/


/**
* @brief          Array holding current status/run-time configuration of individual Hscg controllers
* @details        Array holding current status/run-time configuration of individual Hscg controllers:
*                 - information like Guard bits, Pre-calculated MB INT masks, Storage space for PDU_ID, INT nesting level,
*                 Index of MB buffer being cancelled, Index of the first MB used for Tx
*                 - declared as global variable in "can_IPW.c" file.
*                 See Can_Hscg.c for declaration.
*
*/
extern VAR(Can_HSCG_ControllerStatusType, CAN_HSCG_VAR) Can_HSCG_ControllerStatuses[CAN_HSCG_MAXCTRL_CONFIGURED];



/*==================================================================================================
*                                           MACROS
=====================================================================================================*/
    
/*==================================================================================================
*                                    FUNCTION PROTOTYPES
==================================================================================================*/
FUNC(Hscg_Fw_ReturnType, CAN_HSCG_CODE) Can_Hscg_InitPlatform( void );
FUNC(Hscg_Fw_ReturnType, CAN_HSCG_CODE) Can_Hscg_InitController( void );
FUNC(void, CAN_HSCG_CODE) Can_Hscg_SetMBRuleFilters( VAR(uint8, AUTOMATIC) Controller);
FUNC(void, CAN_HSCG_CODE) Can_Hscg_SetEnhancedFifoFilters( VAR(uint8, AUTOMATIC) Controller);
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_DeInitController( VAR(uint8, AUTOMATIC) Controller);
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_DeInitPlatform( void );
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_ChangeBaudrate( VAR(uint8, AUTOMATIC) Controller);
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_GetControllerMode( VAR(uint8, AUTOMATIC) Controller,
                                                            P2VAR(Can_ControllerStateType, AUTOMATIC, CAN_HSCG_APPL_DATA) ControllerModePtr);
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_SetControllerMode( VAR(uint8, AUTOMATIC) Controller,
                                                            VAR(Can_ControllerStateType, AUTOMATIC) Transition );
FUNC(void, CAN_HSCG_CODE) Can_Hscg_DisableControllerInterrupts( VAR(uint8, AUTOMATIC) Controller);
FUNC(void, CAN_HSCG_CODE) Can_Hscg_EnableControllerInterrupts( VAR(uint8, AUTOMATIC) Controller );
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_Write( Can_HwHandleType Hth,
                                               P2CONST(Can_PduType, AUTOMATIC, CAN_HSCG_APPL_CONST) Pdu);
void Can_Hscg_Start(void);
FUNC(void, CAN_HSCG_CODE) Can_Hscg_ProcessTx( CONST(uint8, AUTOMATIC) u8AckInterface );
#if (CAN_HSCG_ERRORNOTIFPOLL_SUPPORTED == STD_ON)
FUNC(void, CAN_HSCG_CODE) Can_Hscg_ProcessErrorNotification(void);
#endif /* (CAN_HSCG_ERRORNOTIFPOLL_SUPPORTED == STD_ON) */

FUNC(void, CAN_HSCG_CODE) Can_Hscg_ProcessNotificationISR( void );
#if (CAN_HSCG_TXPOLL_SUPPORTED == STD_ON)
FUNC(void, CAN_HSCG_CODE) Can_Hscg_MainFunctionWrite( void );
FUNC(void, CAN_HSCG_CODE) Can_Hscg_MainFunctionMultipleWritePoll( VAR(uint8, AUTOMATIC) writepoll);
#endif /* (CAN_HSCG_TXPOLL_SUPPORTED == STD_ON) */

#if (CAN_HSCG_RXPOLL_SUPPORTED == STD_ON)
FUNC(void, CAN_HSCG_CODE) Can_Hscg_MainFunctionRead( void);
FUNC(void, CAN_HSCG_CODE) Can_Hscg_MainFunctionMultipleReadPoll( VAR(uint8, AUTOMATIC) readpoll);
#endif /* (CAN_HSCG_RXPOLL_SUPPORTED == STD_ON) */
FUNC(void, CAN_HSCG_CODE) Can_Hscg_MainFunctionMode( void);
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_GetControllerErrorState( VAR(uint8, AUTOMATIC) ControllerId, P2VAR(Can_ErrorStateType, CAN_HSCG_VAR, CAN_HSCG_APPL_DATA) ErrorStatePtr );
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_GetControllerRxErrorCounter( VAR(uint8, AUTOMATIC) ControllerId, P2VAR(uint8, AUTOMATIC, CAN_HSCG_APPL_DATA) RxErrorCounterPtr );
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_GetControllerTxErrorCounter( VAR(uint8, AUTOMATIC) ControllerId, P2VAR(uint8, AUTOMATIC, CAN_HSCG_APPL_DATA) TxErrorCounterPtr );
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_GetControllerStatus( VAR(uint8, AUTOMATIC) ControllerId, P2VAR(Hscg_Can_GetStatusCmdType, CAN_HSCG_VAR, CAN_HSCG_APPL_DATA) StatusPtr );
FUNC(Std_ReturnType, CAN_HSCG_CODE) Can_Hscg_GetFwVersion(const uint8 ** pVersionString, P2VAR(uint8, AUTOMATIC, CAN_HSCG_APPL_DATA) pStringLength);






/* Stub functions ---can*/
void CanIf_RxIndication( const Can_HwType* Mailbox, const PduInfoType* PduInfoPtr );
void CanIf_TxConfirmation( PduIdType CanTxPduId);
void CanIf_ControllerModeIndication( uint8 Controller, Can_ControllerStateType ControllerMode);
void CanIf_ControllerBusOff( uint8 Controller);
void RxTimestampNotification(Can_HwHandleType Hoh, uint32 u32TimestampVal);
void TxTimestampNotification(Can_HwHandleType Hoh, PduIdType CanTxPduId, uint32 u32TimestampVal);
void CanErrorNotification(uint8 u8CanIfCtrlId, Hscg_Can_NotificationIdType eNotifId, volatile Hscg_Can_ErrorNotifType* ErrorInfo);











#ifdef __cplusplus
}
#endif

#endif /* CAN_HSCG_H */

/** @} */
